import AdAgent from "./AdAgent";
import { BannerLocation } from "./AdManager";
import PlatUtils from "./PlatUtils";
import { utils } from "./Utils";
import CommonConfig from "./CommonConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class AdAgentWechat extends AdAgent {

    _curBannerAd: BannerAd = null;
    _oldBannerAd: BannerAd = null;

    public Init() {
        if (PlatUtils.IsWechat) {
            // this._createBanner();
            // this._createBanner();
        }
    }

    _createBanner() {
        // 如果是竖屏游戏，banner铺满宽度，如果是横屏游戏，banner按最小300
        let width: number = 300;
        // if (utils.wechatTool.getSystemInfo().screenWidth > utils.wechatTool.getSystemInfo().screenHeight) {
        //     width = 300;
        // } else {
        //     width = cc.winSize.width;
        // }

        let left: number = (utils.wechatTool.getSystemInfo().screenWidth - width) / 2;
        let params = {
            adUnitId: utils.config.wechatconfig.bannerId[0],
            style: {
                left: left,
                top: 0,
                width: width,
                height: 0
            }
        };
        this._curBannerAd = wx.createBannerAd(params);

        this._curBannerAd.onError((err) => {
            cc.log("广告条加载失败! ", err);
        });

        let self = this;
        this._curBannerAd.onResize((res) => {
            cc.log("广告条宽高:", res);
            cc.log(self._curBannerAd.style);
            cc.log(utils.wechatTool.getSystemInfo());
            if (utils.config.wechatconfig.isAlignBottom) {
                self._curBannerAd.style.top = utils.wechatTool.getSystemInfo().screenHeight - res.height;
            }

        });

        this._curBannerAd.onLoad(() => {
            self._curBannerAd.show();
            if(self._oldBannerAd){
                self._oldBannerAd.hide();
                self._oldBannerAd.destroy();
            }
        });
    }

    _createInterstitial() {

    }

    public ShowBanner(location: BannerLocation) {
        if (PlatUtils.IsWechat) {
            cc.log("AdAgentWechat ShowBanner");
            if (this._curBannerAd) {
                let oldBanner = this._curBannerAd;
                oldBanner.destroy();
                // this._oldBannerAd = this._curBannerAd;
            }

            this._createBanner();
        }
    }

    public HideBanner(location: BannerLocation) {
        if (PlatUtils.IsWechat) {
            cc.log("AdAgentWechat HideBanner");
            if (this._curBannerAd) {
                this._curBannerAd.hide();
            }
        }
    }

    public ShowInterstitial() {
        if (PlatUtils.IsWechat) {
            cc.log("AdAgentWechat ShowInterstitial");

        }
    }

    public GameExit() {
        if (PlatUtils.IsWechat) {
            cc.log("AdAgentWechat GameExit");

        }
    }

    public Share() {
        if (PlatUtils.IsWechat) {
            if (utils.wechatTool) {
                utils.wechatTool.share();
            }
        }
    }

}
